package com.xz.group12306.business.service.Impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.xz.group12306.business.dao.BaseDao;
import com.xz.group12306.business.dao.TicketDao;
import com.xz.group12306.business.dao.Impl.TicketDaoImpl;
import com.xz.group12306.business.service.TicketService;
import com.xz.group12306.domain.Cart;
import com.xz.group12306.domain.Ticket;
import com.xz.group12306.utils.ServiceException;

public class TicketServiceImpl implements TicketService{

	@Override
	public List<Ticket> findTicketList(Ticket one,int page,int rows) {
		// TODO Auto-generated method stub
		List<Ticket> tickets=null;
		Connection conn = null;
		try {
			conn = BaseDao.getConnection();
			TicketDao ticketDao = new TicketDaoImpl(conn);
			BaseDao.beginTransaction(conn);
	        tickets=ticketDao.findTicketList(one, page, rows);
	        BaseDao.commit(conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			BaseDao.rollback(conn);
			try {
				throw new ServiceException("查询车票出错", e);
			} catch (ServiceException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			BaseDao.closeConnection(conn);
		}
		return tickets;
	}

	
	@Override
	public Ticket selectOneTicket(int ticketid) {
		// TODO Auto-generated method stub
		Connection conn = null;
		conn=BaseDao.getConnection();
		BaseDao.beginTransaction(conn);
		// TODO Auto-generated method stub
		TicketDaoImpl ticketDaoImpl = new TicketDaoImpl(conn);// 实例化TicketDao的实现类
		Ticket ticket=ticketDaoImpl.selectTicket(ticketid);
		BaseDao.commit(conn);
		BaseDao.closeConnection(conn);
		return ticket;	
	}

	@Override
	public void addCart(Cart cart) {
		// TODO Auto-generated method stub
		Connection conn = null;
		conn=BaseDao.getConnection();
		BaseDao.beginTransaction(conn);
		// TODO Auto-generated method stub
		TicketDaoImpl ticketDaoImpl = new TicketDaoImpl(conn);
		ticketDaoImpl.save(cart);
		BaseDao.commit(conn);
		BaseDao.closeConnection(conn);
	}


	@Override
	public void updateTicket(int ticketid) {
		// TODO Auto-generated method stub
		Connection conn = null;
		conn=BaseDao.getConnection();
		BaseDao.beginTransaction(conn);
		// TODO Auto-generated method stub
		TicketDaoImpl ticketDaoImpl = new TicketDaoImpl(conn);
		ticketDaoImpl.updateTicketNo(ticketid);
		BaseDao.commit(conn);
		BaseDao.closeConnection(conn);
	}

}
